package com.async.boot.task;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;


@Component
@Async("taskExecutor")
@Slf4j
public class AsyncTask {

    public void task1() throws InterruptedException {
        Thread.currentThread().setName("线程1");
        log.info("当前线程名称为{}", Thread.currentThread().getName());
        long begin = System.currentTimeMillis();
//        Thread.sleep(2000L);
        for (int i = 0; i < 1000000; i++) {
            log.info("当前线程名称为{},值为:{}", Thread.currentThread().getName(), i);
        }
        long end = System.currentTimeMillis();
        System.out.println("任务1耗时=" + (end - begin));
    }

    public void task2() throws InterruptedException {
        Thread.currentThread().setName("线程2");
        log.info("当前线程名称为{}", Thread.currentThread().getName());
        long begin = System.currentTimeMillis();
//        Thread.sleep(2000L);
        for (int i = 0; i < 1000000; i++) {
            log.info("当前线程名称为{},值为:{}", Thread.currentThread().getName(), i);
        }
        long end = System.currentTimeMillis();
        System.out.println("任务2耗时=" + (end - begin));
    }

    public void task3() throws InterruptedException {
        Thread.currentThread().setName("线程3");
        log.info("当前线程名称为{}", Thread.currentThread().getName());
        long begin = System.currentTimeMillis();
//        Thread.sleep(2000L);
        for (int i = 0; i < 1000000; i++) {
            log.info("当前线程名称为{},值为:{}", Thread.currentThread().getName(), i);
        }
        long end = System.currentTimeMillis();
        System.out.println("任务3耗时=" + (end - begin));
    }

    //获取异步结果
    public Future<String> task4() throws InterruptedException {
        log.info("当前线程名称为{}", Thread.currentThread().getName());
        long begin = System.currentTimeMillis();
//        Thread.sleep(2000L);
        for (int i = 0; i < 10000; i++) {
            log.info("当前线程名称为{}", Thread.currentThread().getName());
        }
        long end = System.currentTimeMillis();
        System.out.println("任务4耗时=" + (end - begin));
        return new AsyncResult<String>("任务4");
    }


    public Future<String> task5() throws InterruptedException {
        log.info("当前线程名称为{}", Thread.currentThread().getName());
        long begin = System.currentTimeMillis();
//        Thread.sleep(3000L);
        long end = System.currentTimeMillis();
        System.out.println("任务5耗时=" + (end - begin));
        return new AsyncResult<String>("任务5");
    }

    public Future<String> task6() throws InterruptedException {
        log.info("当前线程名称为{}", Thread.currentThread().getName());
        long begin = System.currentTimeMillis();
//        Thread.sleep(1000L);
        long end = System.currentTimeMillis();
        System.out.println("任务6耗时=" + (end - begin));
        return new AsyncResult<String>("任务6");
    }

}
